package org.qydjk.console.shiro;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;

import java.io.Serializable;
import java.util.Collection;

/**
 *
 */
public class ShiroSessionDao extends EnterpriseCacheSessionDAO{
    Logger logger = LogManager.getLogger(ShiroSessionDao.class);

    private JedisShiroSessionRepository jedisShiroSessionRepository;

    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = this.generateSessionId(session);
        this.assignSessionId(session, sessionId);
        getJedisShiroSessionRepository().saveSession(session);
        return sessionId;
    }


    protected void doUpdate(Session session) {
        getJedisShiroSessionRepository().saveSession(session);
    }

    protected void doDelete(Session session) {
        if (session == null) {
            logger.error("doDelete----Session 不能为null");
            return;
        }
        Serializable id = session.getId();
        if (id != null)
            getJedisShiroSessionRepository().deleteSession(id);
    }

    @Override
    public Collection<Session> getActiveSessions() {
        return getJedisShiroSessionRepository().getAllSessions();
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        return getJedisShiroSessionRepository().getSession(sessionId);
    }

    public JedisShiroSessionRepository getJedisShiroSessionRepository() {
        return jedisShiroSessionRepository;
    }

    public void setJedisShiroSessionRepository(JedisShiroSessionRepository jedisShiroSessionRepository) {
        this.jedisShiroSessionRepository = jedisShiroSessionRepository;
    }
}
